package com.card.hwatt.core;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import com.card.hwatt.dto.DataMap;

public final class DataMapUntils {

	private static final String PATH = "map.json";

	public static List<DataMap> loadAllDataMap(HttpServletRequest request) {
		String realPath = request.getSession().getServletContext()
				.getRealPath("/data");
		File dir = new File(realPath);
		if (!dir.exists()) {
			dir.mkdirs();
		}

		String path = realPath + File.separatorChar + PATH;
		File file = new File(path);
		if (!file.exists()) {
			return null;
		}

		try {
			String json = FileUtils.readFileToString(file, "UTF-8");
			ObjectMapper objectMapper = new ObjectMapper();
			List<DataMap> list = objectMapper.readValue(json,
					new TypeReference<List<DataMap>>() {
					});
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
